<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    onList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 判断code
    var validateCode = (rule, value, callback) => {
      let isNan = this.onList
      if (this.isEdit) {
        isNan = this.onList.filter(item => item.id !== this.id)
      }
      isNan.some(item => item.code === value) ? callback(new Error('code重复')) : callback()
    }
    // 判断部门
    var validateName = (rule, value, callback) => {
      if (!this.isEdit) {
        const isNan = this.onList.filter(item => item.pid === this.id)
        isNan.some(item => item.name === value) ? callback(new Error(value + '重复')) : callback()
      } else {
        const pid = this.onList.find(item => item.id === this.id).pid
        const isBji = this.onList.filter(item => item.pid === pid && item.id !== this.id)
        isBji.some(item => item.name === value) ? callback(new Error(value + '重复')) : callback()
      }
    }
    return {
      rules: {
        name: [{ required: true, message: '部门名称不能为空', tirgger: 'blur' }, { validator: validateName, trigger: 'blur' }], // 部门名称
        code: [
          { required: true, message: '部门编码不能为空', tirgger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ], // 部门编码
        manager: [{ required: true, message: '部门管理者不能为空', tirgger: 'change' }], // 部门管理者
        introduce: [{ required: true, message: '部门介绍不能为空', tirgger: 'blur' }] // 部门介绍
      },
      employees: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    }
  },
  created() {
    // 获取员工信息
    this.getEmployeeSimpleApi()
    // 获取部门详情
    if (this.isEdit) {
      this.getDepartDetailApi()
    }
  },
  methods: {
    async getEmployeeSimpleApi() {
      const res = await getEmployeeSimple()
      this.employees = res.data
    },
    // 执行具体的添加动作
    async doAdd() {
      // 去获取pid： 当前要添加的部门所在的父级的id
      // 从props中获取
      // 1. 组装参数
      const params = { ...this.form, pid: this.id }
      console.log(params, this.id)
      // 2. 调用接口
      const res = await addDepartments(params)
      console.log(res)
      // 3. 通知父组件：关闭弹层，再次更新数据
      this.$emit('success')
    },
    // 获取部门详情
    async getDepartDetailApi() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.id)
        this.form = res.data
      }
    },
    // 更新部门
    async doEdit() {
      try {
        await updateDepartments(this.form)
        this.$message({ type: 'success', message: '修改成功' })
        this.$emit('success')
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: err })
      }
    },
    hSubmit() {
      // 手动兜底校验
      this.$refs.form.validate(valid => {
        console.log(valid)
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('close')
    }
    // 清空表单数据
    // resetForm() {
    //   this.$refs.deptForm.resetFields()
    //   console.log('resetForm')
    // }
  }
}
</script>
